/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.hq.mes.bussiness.warehouse.service.impl;

import com.hq.mes.bussiness.warehouse.domain.Warehouse;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.hq.mes.bussiness.warehouse.repository.WarehouseRepository;
import com.hq.mes.bussiness.warehouse.service.WarehouseService;
import com.hq.mes.bussiness.warehouse.service.dto.WarehouseDto;
import com.hq.mes.bussiness.warehouse.service.dto.WarehouseQueryCriteria;
import com.hq.mes.bussiness.warehouse.service.mapstruct.WarehouseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wsq
* @date 2021-12-24
**/
@Service
@RequiredArgsConstructor
public class WarehouseServiceImpl implements WarehouseService {

    private final WarehouseRepository warehouseRepository;
    private final WarehouseMapper warehouseMapper;

    @Override
    public Map<String,Object> queryAll(WarehouseQueryCriteria criteria, Pageable pageable){
        Page<Warehouse> page = warehouseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(warehouseMapper::toDto));
    }

    @Override
    public List<WarehouseDto> queryAll(WarehouseQueryCriteria criteria){
        return warehouseMapper.toDto(warehouseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WarehouseDto findById(Long id) {
        Warehouse warehouse = warehouseRepository.findById(id).orElseGet(Warehouse::new);
        ValidationUtil.isNull(warehouse.getId(),"Warehouse","id",id);
        return warehouseMapper.toDto(warehouse);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WarehouseDto create(WarehouseDto resources) {
        return warehouseMapper.toDto(warehouseRepository.save( warehouseMapper.toEntity(resources)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WarehouseDto resources) {
        Warehouse warehouse = warehouseRepository.findById(resources.getId()).orElseGet(Warehouse::new);
        ValidationUtil.isNull( warehouse.getId(),"Warehouse","id",resources.getId());
        warehouse.copy( warehouseMapper.toEntity(resources));
        warehouseRepository.save(warehouse);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            warehouseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<WarehouseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WarehouseDto warehouse : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编码", warehouse.getCode());
            map.put("名称", warehouse.getName());
            map.put("仓库类型", warehouse.getType());
            map.put("备注", warehouse.getRemark());
            map.put("可用状态", warehouse.getIsEnable());
            map.put("创建人", warehouse.getCreateBy());
            map.put("创建时间", warehouse.getCreateTime());
            map.put("最后修改人", warehouse.getUpdateBy());
            map.put("最后修改时间", warehouse.getUpdateTime());
            map.put("是否已删除", warehouse.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}